package com.ten.mall.utils;

import java.text.SimpleDateFormat;
import java.util.Arrays;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;

import com.ten.mall.vo.OrderReturnVo;
import com.ten.mall.vo.weixin.AccessToken;



public class CommonUtis {
	/**
	 * 生产uuid
	 * @return
	 */
	public static String createId(){
		String uuid =UUID.randomUUID().toString();
	  
		return uuid.replace("-", "");
	}
	/**
	 * 微信校验
	 * @param signature
	 * @param timestamp
	 * @param nonce
	 * @param token
	 * @return
	 */
	public static boolean chechSignature(String signature,String timestamp,
			  String nonce,String token){
		
		String [] arr=new String[] {token,timestamp,nonce};
		//排序
		Arrays.sort(arr);
		StringBuffer sb =new StringBuffer();
		for(String s:arr){
			sb.append(s);
		}
		String temp =JavaDegestUtils.getSha1(sb.toString());
		return signature.equals(temp);
	}
	/**
	 * 产生orderId
	 * @return
	 */
	public static String creatOrderId(){
		SimpleDateFormat format =new SimpleDateFormat("yyyyMMddHHmmss");
		StringBuffer sb=new StringBuffer("SH");
		String ss =format.format(new Date());
		int s=(int)((Math.random()*9+1)*1000);
		sb.append(ss);
		sb.append(s);
		return sb.toString();
	}
	/**
	 * 日期格式化
	 * @param patten
	 * @param date
	 * @return
	 */
	public static String formateDate(String patten,Date date){
		SimpleDateFormat format =new SimpleDateFormat(patten);
		String res =format.format(date);
		return res;
	}
	/**
	 * 日期格式化
	 * @param patten
	 * @param str
	 * @return
	 * @throws Exception
	 */
	public static Date toDate(String patten,String str) throws Exception{
		SimpleDateFormat format =new SimpleDateFormat(patten);
		Date res =format.parse(str);
		return res;
	}
	/**
	 * 导出excel
	 * @param header
	 * @param dataList
	 */
	public static HSSFWorkbook exportExcel(List<String> header,List<OrderReturnVo> dataList,
			String title){
		//创建HSSFWorkbook对象(excel的文档对象)
        HSSFWorkbook wb = new HSSFWorkbook();
        //建立新的sheet对象（excel的表单）
        HSSFSheet sheet=wb.createSheet(title);
      //生成第一行 
       Row row = sheet.createRow((int) 0);
     //生成单元格的样式style 
       
     HSSFCellStyle style = wb.createCellStyle(); 
     style.setAlignment(CellStyle.ALIGN_CENTER);

       for(int i=0;i<header.size();i++){
    	 //创建单元格并设置单元格内容
    	   Cell cell = row.createCell(i);
    	 //给单元格赋值
    	   cell.setCellValue(header.get(i)); 
    	   //设置单元格的样式 
    	   cell.setCellStyle(style);
    	
       }
       
     //在sheet里创建第三行
       for(int j=0;j<dataList.size();j++){
    	   row = sheet.createRow(row.getRowNum() + 1);
    	   row.createCell(0).setCellValue(dataList.get(j).getId());
    	   row.createCell(1).setCellValue(dataList.get(j).getGoodsName());
    	   row.createCell(2).setCellType(HSSFCell.CELL_TYPE_NUMERIC);
    	   row.createCell(2).setCellValue(dataList.get(j).getOrderTotal().doubleValue());
    	   row.createCell(3).setCellType(HSSFCell.CELL_TYPE_NUMERIC);
    	   row.createCell(3).setCellValue(dataList.get(j).getOrderSum());
    	   row.createCell(4).setCellValue(dataList.get(j).getDoctorName());
    	   row.createCell(5).setCellValue(dataList.get(j).getReceiptName());
    	   row.createCell(6).setCellValue(dataList.get(j).getTelephone());
    	   row.createCell(7).setCellValue(dataList.get(j).getAddressDetail());
    	   if(StringUtils.isNotBlank(dataList.get(j).getOrderYuliu())){
    		   row.createCell(8).setCellValue("自取");
    	   }else{
    		   row.createCell(8).setCellValue("配送");
    	   }
    	   row.createCell(9).setCellValue(dataList.get(j).getHouseName());
    	   row.createCell(10).setCellValue(dataList.get(j).getHouseAddress());
    	   row.createCell(11).setCellValue(dataList.get(j).getOrderRemark());
       }
       return wb;
	}
	/*
	 * 导出
	 */
	public static HSSFWorkbook exportGoodsExcel(List<String> header,List<OrderReturnVo> dataList,
			String title){
		//创建HSSFWorkbook对象(excel的文档对象)
        HSSFWorkbook wb = new HSSFWorkbook();
        //建立新的sheet对象（excel的表单）
        HSSFSheet sheet=wb.createSheet(title);
      //生成第一行 
       Row row = sheet.createRow((int) 0);
     //生成单元格的样式style 
       
     HSSFCellStyle style = wb.createCellStyle(); 
     style.setAlignment(CellStyle.ALIGN_CENTER);

       for(int i=0;i<header.size();i++){
    	 //创建单元格并设置单元格内容
    	   Cell cell = row.createCell(i);
    	 //给单元格赋值
    	   cell.setCellValue(header.get(i)); 
    	   //设置单元格的样式 
    	   cell.setCellStyle(style);
    	
       }
       
     //在sheet里创建第三行
       for(int j=0;j<dataList.size();j++){
    	   row = sheet.createRow(row.getRowNum() + 1);
    	   row.createCell(0).setCellValue(dataList.get(j).getId());
    	   row.createCell(1).setCellValue(dataList.get(j).getGoodsName());
    	   row.createCell(2).setCellType(HSSFCell.CELL_TYPE_NUMERIC);
    	   row.createCell(2).setCellValue(dataList.get(j).getOrderTotal().doubleValue());
    	   row.createCell(3).setCellType(HSSFCell.CELL_TYPE_NUMERIC);
    	   row.createCell(3).setCellValue(dataList.get(j).getOrderSum());
    	   row.createCell(4).setCellValue(dataList.get(j).getDoctorName());
    	   row.createCell(5).setCellValue(dataList.get(j).getReceiptName());
    	   row.createCell(6).setCellValue(dataList.get(j).getTelephone());
    	   row.createCell(7).setCellValue(dataList.get(j).getAddressDetail());
    	   if(StringUtils.isNotBlank(dataList.get(j).getOrderYuliu())){
    		   row.createCell(8).setCellValue("自取");
    	   }else{
    		   row.createCell(8).setCellValue("配送");
    	   }
    	   row.createCell(9).setCellValue(dataList.get(j).getHouseName());
    	   row.createCell(10).setCellValue(dataList.get(j).getHouseAddress());
    	   row.createCell(11).setCellValue(dataList.get(j).getOrderRemark());
    	   row.createCell(12).setCellValue(dataList.get(j).getSaleMan());
       }
       return wb;
	}
	/**
	 * 
	 * @param token
	 * @return
	 */
	public static boolean isDisable(AccessToken token){
		boolean flag=false;
		Long l =(new Date().getTime()-token.getCreateTime().getTime())/1000;
		if(l>7100l){
			flag=true; 
		}
		return flag;
	}
}